/*
 * FunctionPrototype.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;
import org.scriptme.ecmascript.interpreter.Evaluator;
import org.scriptme.ecmascript.interpreter.ScopeChain;

/**
 * Implements the prototype and is the class of all Function objects.
 */
public class FunctionPrototype extends ESObject {

	/** The function name. */
	private String functionName = null;

	/** The length. */
	private int length = 0;

	/** The Constant LENGTHstring. */
	private static final String LENGTHstring = ("length").intern();

	/** The Constant LENGTHhash. */
	private static final int LENGTHhash = LENGTHstring.hashCode();

	/**
	 * Instantiates a new function prototype.
	 * 
	 * @param prototype
	 *            the prototype
	 * @param evaluator
	 *            the evaluator
	 * @param functionName
	 *            the function name
	 * @param length
	 *            the length
	 */
	FunctionPrototype(ESObject prototype, Evaluator evaluator,
			String functionName, int length) {
		super(prototype, evaluator);
		this.functionName = functionName;
		this.length = length;
	}

	/**
	 * Instantiates a new function prototype.
	 * 
	 * @param prototype
	 *            the prototype
	 * @param evaluator
	 *            the evaluator
	 * @param length
	 *            the length
	 */
	FunctionPrototype(ESObject prototype, Evaluator evaluator, int length) {
		super(prototype, evaluator);
		this.length = length;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getESClassName()
	 */
	public String getESClassName() {
		return "Function";
	}

	/**
	 * Gets the function name.
	 * 
	 * @return the function name
	 */
	public String getFunctionName() {
		if (functionName == null) {
			return "anonymous";
		} else {
			return functionName;
		}
	}

	/**
	 * get the string defining the function.
	 * 
	 * @return a String indicating that this is the function prototype
	 */
	public String getFunctionImplementationString() {
		return "{<FunctionPrototype (" + this.getClass().getName() + ")>}";
	}

	/**
	 * get the string defining the function.
	 * 
	 * @return a string indicating that the function prototype has no argument
	 */
	public String getFunctionParametersString() {
		return "()";
	}

	/**
	 * Get the number of arguments property.
	 * 
	 * @return the length property
	 */
	public int getLengthProperty() {
		return length;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getPropertyInScope(java.lang.String,
	 *      org.scriptme.ecmascript.interpreter.ScopeChain, int)
	 */
	public ESValue getPropertyInScope(String propertyName,
			ScopeChain previousScope, int hash) throws EcmaScriptException {
		if (hash == LENGTHhash && propertyName.equals(LENGTHstring)) {
			return new ESNumber(length);
		}
		return super.getPropertyInScope(propertyName, previousScope, hash);
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getProperty(java.lang.String,
	 *      int)
	 */
	public ESValue getProperty(String propertyName, int hash)
			throws EcmaScriptException {
		if (hash == LENGTHhash && propertyName.equals(LENGTHstring)) {
			return new ESNumber(length);
		} else {
			return super.getProperty(propertyName, hash);
		}
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#hasProperty(java.lang.String,
	 *      int)
	 */
	public boolean hasProperty(String propertyName, int hash)
			throws EcmaScriptException {
		if (hash == LENGTHhash && propertyName.equals(LENGTHstring)) {
			return true;
		} else {
			return super.hasProperty(propertyName, hash);
		}
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#putProperty(java.lang.String,
	 *      org.scriptme.ecmascript.data.ESValue, int)
	 */
	public void putProperty(String propertyName, ESValue propertyValue, int hash)
			throws EcmaScriptException {
		if (!(hash == LENGTHhash && propertyName.equals(LENGTHstring))) {
			super.putProperty(propertyName, propertyValue, hash);
		} // Allowed via putHiddenProperty, used internally !
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getSpecialPropertyNames()
	 */
	public String[] getSpecialPropertyNames() {
		String[] ns = { LENGTHstring };
		return ns;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#callFunction(org.scriptme.ecmascript.data.ESObject,
	 *      org.scriptme.ecmascript.data.ESValue[])
	 */
	public ESValue callFunction(ESObject thisObject, ESValue[] arguments)
			throws EcmaScriptException {
		return ESUndefined.theUndefined;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getTypeofString()
	 */
	public String getTypeofString() {
		return "function";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#toString()
	 */
	public String toString() {
		return "<" + getESClassName() + ":" + this.getFunctionName() + ">";
	}
}
